<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\PtypeModel;
use app\admin\model\ProductModel;

class Ptype extends Base
{
	/**
	 * 产品类型对象
	 * @return mixed
	 */
	public $ptype_obj;
	
	/**
	 * 产品对象
	 * @return mixed
	 */
	public $product_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->ptype_obj = new PtypeModel(); 
		$this->product_obj = new ProductModel(); 
	}
	
	/**
	 * 产品类型
	 * @return mixed
	 */
    public function lists()
    {
		$input = input('get.');
		if(isset($input['data'])){
			$data = $this->ptype_obj->getPtypeList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			return view('index');
		}
    }
	
	/**
	 * 新增类型页面
	 * @return mixed
	 */
	public function add()
	{
		$size_config = config('size_list');
		$this->assign('size', $size_config);
		return view('add');
	}
	/**
	 * 编辑类型
	 * @return mixed
	 */
	public function edit()
	{
		$input = input('get.');
		$view = PtypeModel::get($input['id'])->toArray();
		$where = array();
		$where['type_id'] = $input['id'];
		$where['rela_status'] = 0;
		$size = $this->ptype_obj->getSizeByType($where);
		$size_config = config('size_list');
		foreach($size_config as $_k=>$_s){
			$size_config[$_k]['value'] = 0;
			if($size['size_'.strtolower($_s['code'])] == 1){
				$size_config[$_k]['value'] = 1;
			}
		}
		$this->assign('size', $size_config);
		$this->assign('view', $view);
		return view('edit');
	}
	
	/**
	 * 更新类型
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			if(isset($input['size_id'])){
				$size_id = $input['size_id'];
				unset($input['size_id']);
			}else{
				$size_id = '';
			}
			if(isset($input['id']) && intval($input['id']) > 0){
				$input['update_time'] = time();
				$flag = $this->ptype_obj->save($input,['id'=>$input['id']]);
				if(!empty($size_id)){
					$input['size_id'] = $size_id;
					$input['type_id'] = $input['id'];
				}
			}else{
				$input['create_time'] = time();
				$input['update_time'] = time();
				$this->ptype_obj->data($input);
				$flag = $this->ptype_obj->save();
				$input['size_id'] = $size_id;
				$input['type_id'] = $this->ptype_obj->id;
			}
			if(isset($input['size_id'])){
				$flag = $this->ptype_obj->insertTypeSize($input);
			}
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新类型数据，请求不合法！');
		}
	}	
	
	/**
	 * 删除类型
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$where = array();
			$where['type_id'] = $input['id'];
			$product = $this->product_obj->getProductList($where);
			if(!empty($country)){
				$result = [
					'code' => 0,
					'msg'  => '产品类型已经关联产品，不允许删除！',
					'list' => $input
				];
			}else{
				$field = PtypeModel::get($input['id']);
				$field->type_status = -1;
				$flag = $field->save();
				if($flag){
					$result = [
						'code' => 1,
						'msg'  => '请求成功',
						'list' => $input
					];
				}else{
					$result = [
						'code' => 0,
						'msg'  => '请求失败',
						'list' => $input
					];
				}
			}
			return json($result);
		}else{
			$this->error('更新类型数据，请求不合法！');
		}
	}
	
	/**
	 * 更新类型状态
	 * @return mixed
	 */
	public function setStatus()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$field = PtypeModel::get($input['id']);
			$status = 1;
			if($field->type_status){
				$status = 0;
			}
			$field->type_status = $status;
			$flag = $field->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新类型数据，请求不合法！');
		}
	}
}
